package core;

import instructions.DatInstruction;
import instructions.Instruction;
import operands.DirectOperand;
import operands.Modifier;

import java.util.ArrayList;

public class Memory 
{
	private final int CORE_SIZE;
	
	private ArrayList<Instruction> memory = new ArrayList<Instruction>();
	
	public Memory(int coreSize)
	{
		CORE_SIZE = coreSize;
		for(int i = 0; i < coreSize; i++)
			memory.add(new DatInstruction(this, Modifier.NON, new DirectOperand(0), new DirectOperand(0)));
	}
	
	public int getAddres(Instruction instruction)
	{
		for(int i = 0; i < CORE_SIZE; i++)
		{
			Instruction current = memory.get(i);
			if(current.equals(instruction))
				return i;
		}
		
		return -1;
	}
	
	public void setInstruction(int i, Instruction instruction)
	{
		i = standardizeAddres(i);
		memory.set(i, instruction);
	}

	public Instruction getInstruction(int i) 
	{
		i = standardizeAddres(i);
		return memory.get(i);
	}
	
	public int standardizeAddres(int i)
	{
		while(i < 0)
			i += CORE_SIZE;
		while(i > CORE_SIZE - 1)
			i -= CORE_SIZE;
		return i;
	}
}
